﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace ToolUtil
{
    public class FtpTool
    {
        private FtpWebRequest reqFTP;
        public string ftppath,username,password;
        public FtpTool(string fpath,string userid,string pass)
        {
            this.ftppath = fpath;
            this.username = userid;
            this.password = pass;
        }

        //连接ftp
        private void Connect(string path)
        {
            // 根据uri创建FtpWebRequest对象
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
            // ftp用户名和密码
            reqFTP.Credentials = new NetworkCredential(username, password);
        }
       
        //判断是否存在该文件
        public Boolean Check(string localFile)
        {
            bool flag = true;
            try
            {
                Connect(this.ftppath + localFile);
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                response.Close();
            }
            catch (Exception ex)
            {
                flag = false;
                Console.WriteLine(ex.Message);
                reqFTP.Abort();
            }
            return flag;
        }
        public Boolean FtpMakeDir(string localFile)
        {
            Connect(this.ftppath + localFile); 
            reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
            try
            {
                reqFTP.UsePassive = true;
                reqFTP.UseBinary = false;
                reqFTP.KeepAlive = false;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                response.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("创建文件报错" + ex.ToString());
                reqFTP.Abort();
                return false;
            }
            reqFTP.Abort();
            return true;
        }
        public bool Rename(string oldFileName, string newFileName)
        {
            bool result = false;
            //建立连接
            Connect(oldFileName);
            reqFTP.Method = WebRequestMethods.Ftp.Rename;
            reqFTP.RenameTo = newFileName;
            using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse())
            {
                result = true;
            }
            return result;
        }
        public string Upload(string filename)
        {
            FileInfo fileInf = new FileInfo(filename);
            string localFile = "/" + DateTime.Now.Year;
            if (!Check(localFile))//判断是否存在该文件夹
            {
                FtpMakeDir(localFile);//创建文件夹 因为一次只能创建一个文件夹
                //Console.WriteLine(localFile);
            }
            localFile = localFile + "/" + DateTime.Now.Month + DateTime.Now.Day;
            if (!Check(localFile))//判断是否存在该文件夹
            {
                FtpMakeDir(localFile);//创建文件夹
                //Console.WriteLine(localFile);
            }
            string uri = this.ftppath + localFile + "/" + fileInf.Name;
            Connect(uri);//连接
            reqFTP.KeepAlive = false;
            reqFTP.Method = "STOR";
            reqFTP.UseBinary = true;
            reqFTP.ContentLength = fileInf.Length;
            // 指定数据传输类型
            reqFTP.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
            int count = 0x5000;
            byte[] buffer = new byte[count];
            try
            {
                FileStream stream = fileInf.OpenRead();
                Stream requestStream = reqFTP.GetRequestStream();
                for (int i = stream.Read(buffer, 0, count); i != 0; i = stream.Read(buffer, 0, count))
                {
                    requestStream.Write(buffer, 0, i);
                }
                requestStream.Close();
                stream.Close();
                return "/bookimg"+localFile + "/" + fileInf.Name;
            }
            catch(Exception ex){ 
                Console.WriteLine(ex.Message);
                return "";
            }
        }
    }
}
